// 订单服务

const orderServicesService = require('../service/orderServicesService');

var express = require('express');
var router = express.Router();




//增加匹配 
router.post('/', async function (req, res) {

    // 获取当前时间戳
    function getLastDate() {
        var date = new Date();
        return Date.parse(date) / 1000;
    }

    // 获取预约时间的时间戳
    let orderDate = new Date(req.body.orderTime);
    function orderTime(orderDate) {
        console.log("预约时间", orderDate);
        return Date.parse(orderDate) / 1000;
    }

    req.body.time = getLastDate();
    req.body.orderTime = orderTime(orderDate);

    console.log(req.body);
    let data = await orderServicesService.insert(req.body)
    res.send(data);
})



//删除匹配
router.delete('/:id', async function (req, res) {
    let data = await orderServicesService.delete(req.params.id)
    res.send(data);
})
//修改匹配
router.put('/:id', async function (req, res) {


    // 当修改的数据只是预约时间时   ， req.body.orderTime 格式必须为 2021-06-19 22:33:00
    if (req.body.orderTime) {  
        // 获取预约时间戳
        let orderDate = new Date(req.body.orderTime);
        function orderTime(orderDate) {
            console.log("预约时间", orderDate);
            return Date.parse(orderDate) / 1000;
        }
        req.body.orderTime = orderTime(orderDate);
    }


    let data = await orderServicesService.update(req.params.id, req.body)
    res.send(data);
})



//分页所有/条件查询
router.get('/all', async function (req, res) {
    let { type, value, current, size, typeId, valueId } = req.query;
    let searchObj = {};
    // console.log(req.query)
    if (type) {
        if (type == "serviceId") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };  // 使用正则可以实现模糊查询
        }
    }else if(typeId){
        searchObj['adminId'] = typeId;
    }
    // console.log(searchObj)
    let data = await orderServicesService.findAll(searchObj, { current: current || 1, size: size || 5 })
    res.send(data);
})

router.get('/as', async function (req, res) {
    let data = await orderServicesService.finds();
    res.send(data);
})






module.exports = router;




